<?php

/**
 * 国际快递悠奇报价 
 */
class Application_Model_ExpressPrice extends Application_Model_DomainObject {
    
    protected $_id;
    
    protected $_expressId;//快递ID
    
    protected $_cityId;//城市ID

    protected $_firstWeight;//首重
    
    protected $_subsequentWeight;//续重
    
    protected $_firstPrice;//首重价(含燃油)
    
    protected $_subsequentPrice;//续重价(含燃油)
    
    protected $_price5;//5KG以上续重价

    protected $_price10;//10KG以上续重价
    
    protected $_price20;//20KG以上续重价
    
    protected $_price40;//40KG以上续重价
    
    protected $_price70;//70KG以上续重价

    protected $_firstFuel;//首重燃油费（只是记录下）
    
    protected $_subsequentFuel;//续重燃油费（只是记录下）
    
    protected $_typeMark;//货物类型掩码 如接收些类型，进行“与”运算后为真,如果为NULL，则取Express的值
    
    protected $_times;//时效

    protected $_firstReport;//官方首重报价
    
    protected $_subsequentReport;//官方续重报价

    protected $_isDisable;//是否禁用
    
    protected $_sortKey;

    protected $_maxkg;  //最大限重(kg)

    protected $_maxcm;  //最大限长(cm)

    protected $_beyond;  //超过按公斤算

    protected $_isWeight;  //1按公斤计算

    /**
     * 国内顺丰标准快件
     */
    const Express_SFStandard = 18;
    /**
     * 国内顺丰特惠
     */
    const Express_SFCheap = 19;
    /**
     * 国内顺丰大宗货物
     */
    const Express_SFBigCargo = 20;

    /**
     * 排序专用字段
     * @var type 
     */
    public $SortKey;

    private $_city;
    public function getCity(){
        if($this->_city == NULL)
            $this->_city = Application_Model_city::load($this->_cityId);
        
        return $this->_city;
    }

    //获取续重价格
    public function getPricingxz()
    {
        return Application_Model_ExpressPricing::getDao()->queryAll(array('expressId' => $this->_expressId, 'countryId' => $this->_cityId , 'type' => 1), 'START ASC');
    }

    //获取公斤价格
    public function getPricinggj()
    {
        return Application_Model_ExpressPricing::getDao()->queryAll(array('expressId' => $this->_expressId, 'countryId' => $this->_cityId , 'type' => 2), 'START ASC');
    }

    /**
     * 返回对应的快递公司
     * @return Application_Model_Express 
     */
    public function getExpress(){
        return Application_Model_Express::load($this->_expressId);
    }

    /**
     * 判断该物流是否接收此类型的货物
     * @param int $type 货物类型
     * @param Application_Model_ExpressPrice $expressPrice
     * @return bool 
     */
    public function isAccept($type){
        return $this->getExpress()->isAccept($type,$this);
    }
    
    /**
     * 根据城市ID返回对应的快递报价
     * @param int $cityId 
     * @return array
     */
    public static function getPricesByCity($cityId){
        return Application_Model_ExpressPrice::getDao()->queryAll(array('cityId'=>$cityId,' and isDisable = '=> 0));
    }
    
    /**
     * 根据重量计算价格
     * @param type $weight 重量
     * @return type  价格
     */
    public function getMoneyByweight($weight){
        $pricing = Application_Model_ExpressPricing::getFreight($this->_expressId, $this->_cityId, $weight);
        //var_dump($pricing);
        //echo $this->_expressId,$this->_cityId,$weight,'<br />';
        if($pricing['status'] < 40){
            return $pricing['totalFee'];
        }else{
            return false;
        }
        //下面为原计算方法
        if($this->_firstWeight>=$weight){
            return $this->_firstPrice;
        }
        $mod = ($weight-($this->_firstWeight)) % $this->_subsequentWeight;//余数
        $baishu = ($weight - $this->_firstWeight - $mod) / $this->_subsequentWeight + ($mod>0?1:0); // 倍数
        $weight = $this->_firstWeight + ($baishu* $this->_subsequentWeight);
        
        
        $price = $this->_subsequentPrice;
        if($weight>=71000){
            $price = $this->_price70;
        }else if($weight>=41000){
            $price = $this->_price40;
        }else if($weight>=21000){
            $price = $this->_price20;
        }else if($weight>=11000){
            $price = $this->_price10;
        }else if($weight>=6000){
            $price = $this->_price5;
        }
        //echo $this->_firstPrice;exit;
        $menoy = $this->_firstPrice + $baishu *$price;
        return $menoy;        
    }
    
    /**
     * 根据重量，返回官方报价
     * @param type $weight
     * @return type 
     */
    public function getReportMoney($weight){
        
        if($this->_firstWeight>=$weight){
            return $this->_firstReport;
        }
        
        $mod = ($weight-($this->_firstWeight)) % $this->_subsequentWeight;//余数
        $baishu = ($weight - $this->_firstWeight - $mod) / $this->_subsequentWeight + ($mod>0?1:0); // 倍数
        $menoy = $this->_firstReport + $baishu *$this->_subsequentReport;
        return $menoy;             
    }
    
    /**
     * 根据快递公司ID一次取起该快递的所有报价
     * @param type $expressId
     * @return array 
     */
    public static function getPricesByExpress($expressId){
        return Application_Model_ExpressPrice::getDao()->queryAll(array('expressId'=>$expressId));
    }
    
    /**
     * 根据快递公司ID与城市ID，取出对像的报价
     * @param type $expressId
     * @param type $cityId
     * @return Application_Model_ExpressPrice 
     */
    public static function getPriceByExpressAndCity($expressId,$cityId){
        return Application_Model_ExpressPrice::getDao()->queryOne(array('expressId'=>$expressId,'cityId'=>$cityId));
    }
    
    
    public static function ByExpressIdType($expressId,$type,$pagesize,$pageindex)
    {
        $where["expressId = "] = $expressId;
        $where[" and isDisable = "] = $type;
        return self::getDao()->queryPage($where, " cityId asc ", $pagesize, $pageindex);
    }
    
    public static function getPriceFilteds($epId,$cityId,$isDisable,$pagesize,$pageindex)
    {
        $where = array();
        $and = "";
        if($epId != "")
        {
            $where["expressId = "] = $epId;
            $and = " and ";
        }
        if($cityId != "")
        {
            $where[$and." cityId = "] = $cityId;
            $and = " and ";
        }
        if($isDisable !== "")
        {
            $where[$and." isDisable = "] = $isDisable;
            $and = " and ";
        }
        return self::getDao()->queryPage($where, " expressId asc ", $pagesize, $pageindex);
    }
    
    public function getDisableName()
    {
        if($this->_isDisable == 1){
            return "禁用";
        } else {
            return "启用";
        }
    }
    
    /**
     *  添加新的渠道价格
     * @param type $epId 渠道ID
     * @param type $cityId 国家ID
     * @param type $firstWeight 首重(g)
     * @param type $subsequentWeight 续重(g)
     * @param type $firstPrice 首重价(含燃油)
     * @param type $subsequentPrice 续重价(含燃油)
     * @param type $isDisable 是否禁用
     * @param type $mintime 最小运输时间
     * @param type $maxtime 最大运输时间
     * @param type $firstReport 官方首重报价
     * @param type $subsequentReport 官方续重报价
     * @param type $firstFuel  首重燃油费（只是记录下）
     * @param type $subsequentFuel 续重燃油费（只是记录下）
     * @param type $price5 5KG以上续重价
     * @param type $price10 10KG以上续重价
     * @param type $price20 20KG以上续重价
     * @param type $price40 40KG以上续重价
     * @param type $price70 70KG以上续重价
     * @return \Application_Model_ExpressPrice
     */
    public static function addExpressPrice($epId,$cityId,$firstWeight,$subsequentWeight,$firstPrice,$subsequentPrice,$typeMark,$isDisable,$mintime,$maxtime,$firstReport,$subsequentReport,$firstFuel,$subsequentFuel,$price5,$price10,$price20,$price40,$price70, $maxkg = 0, $maxcm = 0, $beyond = 0, $isWeight = 0, $priceId = 0)
    {
        try {
            $db = Application_Model_OrderItem::getDao();
            $db->beginTran();
            $priceItem = new Application_Model_ExpressPrice();
            if(!empty($priceId) && is_numeric($priceId)){
                $priceItem->Id = $priceId;
            }
            $priceItem->ExpressId = $epId;
            $priceItem->CityId = $cityId;
            $priceItem->FirstWeight = $firstWeight;
            $priceItem->SubsequentWeight = $subsequentWeight;
            $priceItem->FirstPrice = number_format($firstPrice,2);
            $priceItem->SubsequentPrice = number_format($subsequentPrice,2);
            $priceItem->typeMark = $typeMark;
            $priceItem->IsDisable = $isDisable;
            $priceItem->Times = $mintime."~".$maxtime;
            $priceItem->FirstReport = number_format($firstReport,2);
            $priceItem->SubsequentReport = number_format($subsequentReport,2);
            $priceItem->firstFuel = $firstFuel; // 暂未用到 默认
            $priceItem->subsequentFuel = $subsequentFuel;//暂未用到 默认
            $priceItem->Price5 = number_format($price5,2);
            $priceItem->Price10 = number_format(($price10?$price10:$priceItem->Price5),2);
            $priceItem->Price20 = number_format(($price20?$price20:$priceItem->Price10),2);
            $priceItem->Price40 = number_format(($price40?$price40:$priceItem->Price20),2);
            $priceItem->Price70 = number_format(($price70?$price70:$priceItem->Price40),2);
            $priceItem->Maxkg    = number_format($maxkg,  2);   //最大重量
            $priceItem->Maxcm    = number_format($maxcm,  2);   //最大长度
            $priceItem->Beyond   = number_format($beyond, 2);   //超过按公斤算
            $priceItem->IsWeight = $isWeight;                   //1按公斤计算
            $priceItem->save();
            $db->commit();
            return $priceItem;
        } catch (Exception $exc) {
            $db->rollBack();
            return 0;
        }
    }
    

    /**
     *  添加新的渠道价格
     * @param type $epId                渠道ID
     * @param type $cityId              国家ID
     * @param type $FirstWeight         首重(g)
     * @param type $SubsequentWeight    续重(g)
     * @param type $firstPrice          首重价
     * @param type $typeMark            货物编码,可过敏感
     * @param type $isDisable           是否禁用
     * @param type $maxkg               最大重量
     * @param type $maxcm               最大长度
     * @param type $beyond              超过按公斤算
     * @param type $isWeight            1按公斤计算
     * @param type $priceId             不为0做更新
     * @return \Application_Model_ExpressPrice
     */
    public static function addNewExpressPrice($epId, $cityId, $firstWeight, $subsequentWeight, $firstPrice, $isDisable, $maxkg, $maxcm, $beyond, $isWeight, $priceId = 0)
    {
        try {
            $db = Application_Model_OrderItem::getDao();
            $db->beginTran();
            $priceItem = new Application_Model_ExpressPrice();
            if(!empty($priceId) && is_numeric($priceId)){
                $priceItem->Id = $priceId;
            }
            $priceItem->ExpressId   = $epId;                        //渠道ID
            $priceItem->CityId      = $cityId;                      //国家ID
            $priceItem->FirstWeight = $firstWeight;                 //首重(g)
            $priceItem->SubsequentWeight    = $subsequentWeight;    //续重(g)
            $priceItem->FirstPrice  = number_format($firstPrice,2); //首重价
            $priceItem->IsDisable   = $isDisable;                   //是否禁用
            $priceItem->Maxkg       = number_format($maxkg,  2);    //最大重量
            $priceItem->Maxcm       = number_format($maxcm,  2);    //最大长度
            $priceItem->Beyond      = empty($beyond) ? 0 : number_format($beyond, 2);    //超过按公斤算
            $priceItem->IsWeight    = $isWeight;                    //1按公斤计算
            $priceItem->typeMark            = 0; //舍弃 可过敏感类型
            $priceItem->SubsequentPrice     = 0; //舍弃 续重价
            $priceItem->Times               = 0; //舍弃 运输时间
            $priceItem->FirstReport         = 0; //舍弃 官方首重报价
            $priceItem->SubsequentReport    = 0; //舍弃 官方续重报价
            $priceItem->firstFuel           = 0; //舍弃
            $priceItem->subsequentFuel      = 0; //舍弃
            $priceItem->Price5              = 0; //舍弃
            $priceItem->Price10             = 0; //舍弃
            $priceItem->Price20             = 0; //舍弃
            $priceItem->Price40             = 0; //舍弃
            $priceItem->Price70             = 0; //舍弃
            $priceItem->save();
            $db->commit();
            return $priceItem;
        } catch (Exception $exc) {
            $db->rollBack();
            return 0;
        }
    }

}

